package com.example.my_music_palyer.controller;

import com.example.my_music_palyer.model.User;
import com.example.my_music_palyer.service.ImageService;
import com.example.my_music_palyer.service.UserService;
import com.example.my_music_palyer.common.tools.BCryptTest;
import com.example.my_music_palyer.common.tools.Constant;
import com.example.my_music_palyer.common.tools.ResponseBodyMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@RequestMapping("/user")
@RestController
@ResponseBody
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private BCryptPasswordEncoder bCryptPasswordEncoder;

    @Autowired
    private ImageService imageService;


    @RequestMapping("/login")
    public ResponseBodyMessage<User> login(String username, String password, HttpServletRequest req) {
        User user = userService.selectByUserName(username);
        if (user != null) {
            //用户存在
            //使用BCrypt加密
            req.getSession().setAttribute(Constant.USERINFO_SESSION_KEY, user);
            boolean flg = bCryptPasswordEncoder.matches(password, user.getPassword());
            user.setPassword("");
            if (!flg) {
                return new ResponseBodyMessage<User>(-1, "密码或用户名错误", user);
            }

            //校验通过，存储session
            HttpSession session = req.getSession(true);
            session.setAttribute("user", user);

            return new ResponseBodyMessage<User>(200, "登陆成功!", user);

        } else {
            User failUser = new User();
            failUser.setId(-1);
            failUser.setUsername(username);
            failUser.setPassword("");
            return new ResponseBodyMessage<User>(-1, "用户不存在!", failUser);

        }
    }

    @RequestMapping("/register")
    ResponseBodyMessage<User> register(String username, String password) {
        User user1 = userService.selectByUserName(username);
        if (user1 != null) {
            //用户名已经被注册
            user1.setPassword("");
            return new ResponseBodyMessage<>(-1, "用户名已被注册", user1);
        }

        //注册此用户名
        User user = new User();
        user.setUsername(username);


        //加密并且注册到数据库中
        user.setPassword(BCryptTest.getPassword(password));
        int flg = userService.registerUser(user);
        if (flg == 1) {
            System.out.println("用户注册成功!");
            user.setPassword("");

            //设置用户初始图像
            User initUser = userService.selectByUserName(username);
            imageService.uploadImage("1.jpg", initUser);

            return new ResponseBodyMessage<>(200, "用户注册成功", user);
        } else {
            User failUser = new User();
            failUser.setUsername(username);
            return new ResponseBodyMessage<>(-1, "内部错误请联系管理员", failUser);
        }


    }

    @RequestMapping("/out")
     void out(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession(false);
        User loginUser = (User) session.getAttribute("user");

        User user = new User();
        System.out.println(loginUser.getUsername()+" 退出！！！！！！！！！");

        session.invalidate();
        resp.sendRedirect("/login.html");

    }


}




